package com.tian.api;

import com.alibaba.fastjson.JSON;
import com.tian.common.ReturnCodeEnum;
import com.tian.common.ReturnMessageDTO;
import com.tian.po.UserPO;
import com.tian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/api/user")
public class UserApiController {
    @Autowired
    private UserService userService;
    static Map<Long, UserPO> userMap = Collections.synchronizedMap(new HashMap<>());

    @RequestMapping(value = "/getUsers", method = RequestMethod.GET)
    public List<UserPO> getUsers(){
        List<UserPO> users = new ArrayList<>(userMap.values());
        return users;
    }

    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public String addUser(@ModelAttribute UserPO user){
        userMap.put(user.getId(), user);
        return "success";
    }

    @RequestMapping(value = "/getUser/{id}", method = RequestMethod.GET)
    public UserPO getUserById(@PathVariable("id") Long id){
        return userMap.get(id);
    }

    @PostMapping("/push")
    public ReturnMessageDTO push(@RequestBody String body){
        ReturnMessageDTO returnMessage = new ReturnMessageDTO();
        List<UserPO> userList = JSON.parseArray(body, UserPO.class);
        System.out.println(JSON.toJSONString(userList));
        returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
        returnMessage.setReturnNote("同步成功");
        return returnMessage;
    }
}
